﻿using System;
using System.Collections;
using System.Collections.Generic;
/******************************************************************************************************************
 * 
 * 
 * 说　明： 参数集合(版本：Version1.0.0)
 * 作　者：李朝强
 * 日　期：2015/05/19
 * 修　改：
 * 参　考：http://my.oschina.net/lichaoqiang/
 * 备　注：暂无...
 * 
 * 
 * ***************************************************************************************************************/
namespace SQLMap.Core
{

    /// <summary>
    /// <![CDATA[ParameterCollection 参数集合]]>
    /// </summary>
    [Serializable()]
    public class ParameterCollection : IList<Parameter>, ICollection<Parameter>, IEnumerable<Parameter>, IEnumerable
    {

        /// <summary>
        /// 全局变量 参数数组
        /// </summary>
        private Parameter[] _Parameters = null;

        /// <summary>
        /// <![CDATA[构造函数]]>
        /// </summary>
        public ParameterCollection()
        {
            _Parameters = new Parameter[0];
        }

        /// <summary>
        /// <![CDATA[索引器]]>
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public Parameter this[int index]
        {
            get { return _Parameters[index]; }
            set { _Parameters[index] = value; }
        }

        /// <summary>
        /// <![CDATA[获取元素个数]]>
        /// </summary>
        public int Count
        {
            get { return _Parameters.Length; }
        }

        /// <summary>
        /// <![CDATA[是否只读]]>
        /// </summary>
        public bool IsReadOnly { get { return false; } }


        /// <summary>
        /// <![CDATA[添加一个元素]]>
        /// </summary>
        /// <param name="item"></param>
        public void Add(Parameter item)
        {
            Array.Resize<Parameter>(ref _Parameters, _Parameters.Length + 1);//重置大小
            _Parameters[_Parameters.Length - 1] = item;
        }

        /// <summary>
        /// <![CDATA[清空元素]]>
        /// </summary>
        public void Clear()
        { 
            Array.Resize<Parameter>(ref _Parameters, 0);//重置大小
        }

        /// <summary>
        /// <![CDATA[是否包含元素]]>
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Contains(Parameter item)
        {
            return (IndexOf(item) > -1);
        }

        /// <summary>
        ///<![CDATA[拷贝元素]]>
        /// </summary>
        /// <param name="array"></param>
        /// <param name="arrayIndex"></param>
        public void CopyTo(Parameter[] array, int arrayIndex)
        {
            array.CopyTo(array, index: arrayIndex);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerator<Parameter> GetEnumerator()
        {
            foreach (var item in _Parameters)
                yield return item;
        }

        /// <summary>
        /// <![CDATA[返回元素在集合中的索引]]>
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int IndexOf(Parameter item)
        {
            return Array.IndexOf(_Parameters, item);
        }

        /// <summary>
        /// <![CDATA[插入一个元素]]>
        /// </summary>
        /// <param name="index"></param>
        /// <param name="item"></param>
        public void Insert(int index, Parameter item)
        {
            Array.Resize<Parameter>(ref _Parameters, _Parameters.Length + 1);//重置大小
            for (var i = _Parameters.Length - 1; i < _Parameters.Length; i--)
            {
                if (i == index)
                {
                    _Parameters[i] = item;
                    break;
                }
                _Parameters[i] = _Parameters[i - 1];
            }
        }

        /// <summary>
        /// <![CDATA[移除一个元素]]>
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Remove(Parameter item)
        {
            int i = IndexOf(item);//在数组中的索引
            if (i < 0) return false;
            RemoveAt(i);
            return true;
        }

        /// <summary>
        /// <![CDATA[移除指定位置的元素]]>
        /// </summary>
        /// <param name="index"></param>
        public void RemoveAt(int index)
        {
            if (index < 0) return;
            for (int j = 0; j < Count; j++)
            {
                if (j == index)
                {
                    _Parameters[j] = null;
                }
                else if (j > index)
                {
                    _Parameters[j] = _Parameters[j - 1];
                }
            }
            Array.Resize(ref _Parameters, Count - 1);
        }

        /// <summary>
        /// <![CDATA[获取迭代器]]>
        /// </summary>
        /// <returns></returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return _Parameters.GetEnumerator();
        }
    }
}